/**
 * This menubar provides a menu change the providers of map, geocoding and route services of the application.
 * All services work independent, so the user can mix the providers of all services.
 * 
 * @author Kim Pommert
 * @version %I%, %G%
 */
qx.Class.define("qooxdoosample.cocktail.ui.ProviderMenuBar",{
	
	extend: qx.ui.menubar.MenuBar,
	
	/**
	 * Class constructor.
	 * Instantiates an object of that class.
	 * 
	 * @param panel		{qooxdoosample.cocktail.ui.AddressPanel}
	 * 						The address panel of the application
	 * @param toolbar	{qooxdoosample.cocktail.ui.MapControlToolbar} 
	 * 						The map control toolbar of the application
	 */
	construct: function(panel, toolbar)
	{
		this.base(arguments);

		this.setToolbar(toolbar);
		this.setAddressPanel(panel);
	},
	
	properties :
	{     	
      	/**
      	 * The current map object of the application.
      	 */
      	map:
      	{
      		nullable: true,
      		init: null,
      		check: "qooxdoosample.cocktail.maps.IMap"
      	},
    	
    	/**
    	 * The current geocoding object of the application.
    	 */
    	geocoder :
    	{
      		nullable : true,
      		init : null,
      		check: "qooxdoosample.cocktail.maps.IGeoCoder"
      	},
      	
      	/**
      	 * The current route planning object of the application.
      	 */
      	route:
      	{
      		nullable: true,
      		init: null,
      		check: "qooxdoosample.cocktail.maps.IRoutePlanner"
      	},
      	
      	/**
      	 * The map control toolbar of the application.
      	 */
      	toolbar:
      	{
      		nullable: true,
      		init: null,
      		check: "qooxdoosample.cocktail.ui.MapControlToolbar"
      	},
      	
      	/**
      	 * The address panel of the application.
      	 */
      	addressPanel:
      	{
      		nullable: true,
      		init: null,
      		check: "qooxdoosample.cocktail.ui.AddressPanel"
      	}
	},
	
	
	members: 
	{		
		/**
		 * Initialize all menus of the manubar
		 */
		init: function()
		{
			this.initMapComponents();
			var mapMenu = new qx.ui.menubar.Button("Map", null, this.getProviderMenu(this.changeMapProvider));
      		var geocoderMenu = new qx.ui.menubar.Button("Geocoder", null, this.getProviderMenu(this.changeGeocoderProvider));
      		var routeMenu = new qx.ui.menubar.Button("Route", null, this.getProviderMenu(this.changeRouteProvider));

	      	this.add(mapMenu);
    	  	this.add(geocoderMenu);
      		this.add(routeMenu);
		},
		
		/**
		 * Initializes the submenu for the menubutton. 
		 * As all three services have the same providers, the submenu for all buttons is the same.
		 * Only the event listener for each menu is different, 
		 * so it has to be specified seperately for all menus.
		 * 
		 * @param listener 	{Function} The function that will be called 
		 * 						when the changeSelected event is fired on that menu
		 * 
		 * @return {qx.ui.menu.Menu} The submenu containing all available providers
		 */
		getProviderMenu: function(listener)
		{
			var menu = new qx.ui.menu.Menu;
			var radioButtonGroup = new qx.ui.form.RadioGroup();
      		var rbGoogle = new qx.ui.menu.RadioButton("Google");
      		var rbBing = new qx.ui.menu.RadioButton("Bing");
      		
      		radioButtonGroup.add(rbGoogle);
      		radioButtonGroup.add(rbBing);
			radioButtonGroup.addListener("changeSelected", listener, this);

      		menu.add(rbGoogle);
      		menu.add(rbBing);
      		return menu;
		},
		
		/**
		 * This function is called, when a new provider is selected for the map service.
		 * 
		 * @param e	{qx.event.type.Data} The event that is fired.
		 */
		changeMapProvider: function(e)
		{
      		var provider = e.getData().getLabel().toLowerCase();
			    		
			this.setMap(qooxdoosample.cocktail.maps.MapFactory.createMap("map", provider));
   			this.getMap().setCenter(new qooxdoosample.cocktail.util.LatLng(0, 0));
   			this.getMap().setZoomLevel(1);
    		
    		this.updateProvider();
		},
		
		/**
		 * This function is called, when a new provider is selected for the geocoding service.
		 * 
		 * @param e	{qx.event.type.Data} The event that is fired.
		 */
		changeGeocoderProvider: function(e)
		{
      		var provider = e.getData().getLabel().toLowerCase();
      		
      		this.setGeocoder(qooxdoosample.cocktail.maps.GeoCoderFactory.createGeoCoder(provider));
      		this.updateProvider();
		},
		
		/**
		 * This function is called, when a new provider is selected for the route service.
		 * 
		 * @param e	{qx.event.type.Data} The event that is fired.
		 */
		changeRouteProvider: function(e)
		{
      		var provider = e.getData().getLabel().toLowerCase();
      		
      		this.setRoute(qooxdoosample.cocktail.maps.RoutePlannerFactory.createRoutePlanner(provider));
      		this.updateProvider();
		},
		
		/**
		 * After every provider change, this method has to be called.
		 * It updates the map, geocoding and route objects on the address list and the control toolbar.
		 */
		updateProvider: function()
		{
			this.getToolbar().updateMap(this.getMap());
			this.getAddressPanel().updateServices(this.getMap(), this.getGeocoder(), this.getRoute());
		},
		
		/**
		 * This method is called on the startup of the application.
		 * As the map has to be deployed on a valid, existing <div> node of the DOM,
		 * the map component has to be initialized after the GUI.
		 * All services will be loaded with default provider that is configured in the factory classes.
		 */
		initMapComponents: function()
		{
    		this.setMap(qooxdoosample.cocktail.maps.MapFactory.createMap("map"));
   			this.getMap().setCenter(new qooxdoosample.cocktail.util.LatLng(0, 0));
   			this.getMap().setZoomLevel(1);
   				
   			this.setGeocoder(qooxdoosample.cocktail.maps.GeoCoderFactory.createGeoCoder());
   			this.setRoute(qooxdoosample.cocktail.maps.RoutePlannerFactory.createRoutePlanner());
		}
		
	}
});

